package com.province.platform.common.utils;

import com.alibaba.fastjson2.JSON;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StrUtils {
    private final static Pattern VARIABLE_PATTERN = Pattern.compile("(\\$)\\{(\\w+)\\}?");

    private final static Pattern URL_PATTERN = Pattern.compile("http://([\\d\\.]+:\\d+)");

    private StrUtils() {}

    public static Set<String> getVariables(final String str) {
        Set<String> variables = new HashSet<>();

        Matcher matcher = VARIABLE_PATTERN.matcher(str);
        while (matcher.find()) {
            variables.add(matcher.group(2));
        }

        return variables;
    }

    public static String getIpAddress(final String url) {
        Matcher matcher = URL_PATTERN.matcher(url);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }

    public static void main(String[] args) {
        Matcher matcher = URL_PATTERN.matcher("http://172.16.31.12:7001/gdCardZks/services/CardService");
        if (matcher.find()) {
            System.out.println(matcher.group(1));
        }
    }
}
